package com.yjx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yjx.service.UserTokenService;
import com.yjx.util.RedisUtil;
import com.yjx.util.TokenUtil;
import com.yjx.vo.UserVo;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

public class UserTokenServiceImpl implements UserTokenService {
    @Value("${timeout}")
    private String timeout;
    @Resource
    private RedisUtil redisUtil;
    @Override
    public UserVo createUserLoginToken(UserVo userVo) {
        //生成token
        String token = TokenUtil.createToken(userVo.getLoginName());
        //放到UserVo
        userVo.setToken(token);
        //放到Redis
        redisUtil.put(token, JSONObject.toJSONString(userVo),Long.valueOf(timeout), TimeUnit.HOURS);
        return userVo;
    }

    @Override
    public String getUserByToken(String token) {
        String userJons = redisUtil.get(token);
        return userJons;
    }
}
